// 计算动态时间
export const useCalTime = (val: any) => {
  // console.log(val)
  const nowYear = new Date().getFullYear()
  const nowMonth = new Date().getMonth() + 1
  const nowDay = new Date().getDate()
  const nowHour = new Date().getHours()

  let newDate = getNewDate(val)
  const getYear = new Date(newDate).getFullYear()
  const getMonth = new Date(newDate).getMonth() + 1
  const getDay = new Date(newDate).getDate()
  const getHour = new Date(newDate).getHours()

  // console.log(nowYear, nowMonth, nowDay, getYear, getMonth, getDay)
  // console.log(nowMonth, nowDay, getMonth, getDay)
  // 如果是今年发布的，返回几天或几月前
  if (nowYear == getYear) {
    if (nowMonth == getMonth) {
      if (nowDay == getDay) {
        if (nowHour == getHour) {
          return '刚刚'
        } else {
          return `${Math.abs(nowHour - getHour)}小时前`
        }
      } else {
        return `${Math.abs(getDay - nowDay)}天前`
      }
    } else {
      return `${nowMonth - getMonth}个月前`
    }
  } else {
    // 如果不是，直接返回几年前
    return `${nowYear - getYear}年前`
  }
}

export const getNewDate = (data: any) => {
  if (typeof data === 'string') {
    let arr = data.split(/[- : \/]/)
    // 月份特殊处理
    if (arr[1] !== undefined || arr[1] !== null) {
      arr[1] = arr[1] - 1
    }
    return new Date(...arr)
  }
  return new Date(data || new Date())
}
